<template>
  <div>
    <el-row>
        <b>创建时间：</b>
        <el-date-picker
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width: 250px;"
            v-model="time"
            >
        </el-date-picker>
        <el-button type="primary" icon="el-icon-search">查询</el-button>
    </el-row>
    <el-row>
        <b>发布状态：</b>
        <el-radio-group v-model="radio1" @change="changeStatus">
        <el-radio-button label="0" value>全部</el-radio-button>
        <el-radio-button label="1">未发布</el-radio-button>
        <el-radio-button label="2">已发布</el-radio-button>
        <el-radio-button label="3">已删除</el-radio-button>
        </el-radio-group>
    </el-row>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="文章编号">
      </el-table-column>
      <el-table-column
        prop="title"
        label="文章标题"
        width="500">
      </el-table-column>
      <el-table-column
        prop="status"
        label="发布状态">
        <template slot-scope="scope">
            <el-tag :type="scope.row.tagColor">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="creator"
        label="创建人">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        label="发布时间">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-search" @click="viewArticle(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        :title="this.articleTitle"
        :visible.sync="dialogVisible"
        width="30%">
        <span>{{ this.articleTitle }}</span>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
    name:'notice',
    data(){
        return{
            radio1: "0",
            tableData: [],
            currentPage:1,
            pageSize:10,
            total:0,
            time:[],
            dialogVisible:false,
            articleTitle:'',
            articleContant:''
        }
    },
    created(){
        this.initAuthority()
    },
    methods:{
        initAuthority(){
        this.$request.get("/role/authority",{
          params:{
            id: JSON.parse(localStorage.getItem("admin")).id,
            index: 3
          }
        })
        .then(res =>{
          this.Authority = res.data
          if(res.data == '1'){
            this.initTable()
          }else{
            this.$message.error("你没有权限访问此功能")
          }
        })
        .catch(err =>{
          console.log(err)
        })

      },
        changeStatus(value){
            console.log(value)
            this.initTable()
        },
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.pageSize = val
            this.initTable()
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.currentPage = val
            this.initTable()
        },
        initTable(){

            let startTime;
            let endTime;
            if(this.time == null){
                startTime = ''
                endTime = ''
            }else{
                startTime = this.time[0];
                endTime = this.time[1];
            }
            this.$request.get("/notice/page",{
                params:{
                    page: this.currentPage,
                    size: this.pageSize,
                    startTime: startTime,
                    endTime: endTime,
                    status:this.radio1
                }
            }).then(res => {
                this.tableData = res.data.records
                this.total = res.data.total
            }).catch(err =>{
                console.log(err)
            })
        },
        viewArticle(row){
            console.log(row)
            this.articleTitle = row.title
            this.articleContant = row.content
            this.dialogVisible = true
        }
    }
}
</script>

<style>
    .el-row{
        margin-bottom: 20px;
    }
</style>